package com.lanou.musicproject.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.alibaba.fastjson.JSON;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.lanou.musicproject.R;
import com.lanou.musicproject.adapter.AlbumListViewAdapter;
import com.lanou.musicproject.bean.Album;
import com.lanou.musicproject.utils.MyConfig;
import com.lanou.musicproject.volley.VolleySingleton;

import org.json.JSONObject;

import java.util.List;

/**
 * Created by dllo on 15/10/22.
 * 搜索中专辑页面
 */
public class AlbumFragment extends Fragment {
    //专辑
    private String url;
    private ListView album_listView;
    private VolleySingleton volleySingleton;
    private List<Album.DataEntity> data;
    private AlbumListViewAdapter adapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.album_fragment,null);
        album_listView = (ListView) view.findViewById(R.id.album_listview);
        volleySingleton = VolleySingleton.getInstance(getActivity());

        initData();


        return view;
    }

    private void initData() {
        //得到Seekcontent中加入缓存的值
        String q = MyConfig.getSharePreStr(getActivity(), "Seekcontent", "url");
        Log.i("dsadsaddasdasdsadsadsa", "++++++++++++++" + q);
        url = "http://api.dongting.com/misc/search/album?uid=866058021842951&f=f3040&app=ttpod&hid=1135212123029932" +
                "&rom=Lenovo%252Fsisleylt%252Fsisleylt%253A4.4.4%252FKTU84P%252FVIBEUI_V2.0_1511_5.79.1_ST_S60-t%253Auser%252Frelease-keys" +
                "&alf=alf203200&cpu=msm8916&resolution=720x1280&net=2&size=50&v=v8.2.0.2015091720&utdid=VSeGyjss0fEDAOqIktP5W%2BO4" +
                "&s=s200&ram=1960896+kB&page=1&q=" +
               q +
                "&active=0&tid=0&mid=Lenovo%2BS60-t&imsi=460078098497828&splus=4.4.4%252F19&cpu_model=Qualcomm+Technologies%2C+Inc+MSM8916";
        JsonObjectRequest jr = new JsonObjectRequest(url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                String str = response.toString();
                Album album = JSON.parseObject(str, Album.class);
                data = album.getData();
                adapter = new AlbumListViewAdapter(getActivity(), data);
                album_listView.setAdapter(adapter);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        volleySingleton.addQueue(jr);
    }
    }

